package com.zking.web.ZC;

import com.zking.dao.domain.Patient;
import com.zking.service.Patient_LoginService;


import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/tologin")
public class front_indexController {
    @Autowired
    private Patient_LoginService patientService;


    @RequestMapping(value = "/login", method= RequestMethod.POST)
    public String submitLogin(String phone, String password, HttpServletRequest request){
            System.out.println("phone="+phone);
            System.out.println("password="+password);
            String hashName ="MD5";
            Object salt = null;
            int hashtime = 999;
            String a = String.valueOf(new SimpleHash(hashName,password,salt,hashtime));
            Patient patient1 = new Patient();
            patient1.setPhone(phone);
            patient1.setPassword(a);
            Patient  patient2= patientService.getPatient(patient1);

            if (patient2!=null){
                String password2 =patient2.getPassword();
                System.out.println("patient2.getPassword="+password2);
                String phone2 = patient2.getPhone();
                System.out.println("phone2="+phone2);
                    System.out.println("电话正确");
                    String b="469b73fc722ee6a978f5e08a11c1047b";
                    if(a!=b&&a.equals(password2)){
                        System.out.println("密码正确");

                        HttpSession session = request.getSession();
                        // 2.将用户名保存在session中
                        session.setAttribute("msg", patient2);

                        return "redirect:/toIndex" ;
                    }else{
                        request.setAttribute("loginError", "密码错误或为空");          // 设置错误属性

                        return "Front_page/front_index";
                    }
                }else{
                    System.out.println("电话不存在");
                    request.setAttribute("loginError", "电话不存在,请重新输入");          // 设置错误属性
                    return "Front_page/front_index";
                }

    }
@RequestMapping("/register")
    public String register(){ return "Front_page/register"; }
}
